package org.example.controller;


import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.example.entity.Road;
import org.example.service.RoadService;
import org.example.util.ResponseResult;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.io.Serializable;
import java.util.List;

/**
 * (Road)表控制层
 *
 * @author makejava
 * @since 2025-02-08 11:39:05
 */
@RestController
@RequestMapping("road")
public class RoadController {
    /**
     * 服务对象
     */
    @Resource
    private RoadService roadService;

    /**
     * 分页查询所有数据
     *
     * @param page 分页对象
     * @param road 查询实体
     * @return 所有数据
     */
    @GetMapping("list")
    public ResponseResult selectAll(Page<Road> page, Road road) {
        Page<Road> roadPage = this.roadService.page(page, new LambdaQueryWrapper<Road>()
                        .like(StrUtil.isNotBlank(road.getRoadName()),Road::getRoadName,road.getRoadName())
                        .like(StrUtil.isNotBlank(road.getManagementType()),Road::getManagementType,road.getManagementType())
                        .like(StrUtil.isNotBlank(road.getRoadGrade()),Road::getRoadGrade,road.getRoadGrade())
                );
        return ResponseResult.ok(roadPage);
    }

    /**
     * 通过主键查询单条数据
     *
     * @param id 主键
     * @return 单条数据
     */
    @GetMapping("{id}")
    public ResponseResult selectOne(@PathVariable Serializable id) {
        return ResponseResult.ok(this.roadService.getById(id));
    }

    /**
     * 新增数据
     *
     * @param road 实体对象
     * @return 新增结果
     */
    @PostMapping("insert")
    public ResponseResult insert(@RequestBody Road road) {
        return ResponseResult.ok(this.roadService.save(road));
    }

    /**
     * 修改数据
     *
     * @param road 实体对象
     * @return 修改结果
     */
    @PostMapping("update")
    public ResponseResult update(@RequestBody Road road) {
        return ResponseResult.ok(this.roadService.updateById(road));
    }

    /**
     * 删除数据
     *
     * @param idList 主键结合
     * @return 删除结果
     */
    @GetMapping("delete")
    public ResponseResult delete(@RequestParam("idList") List<Long> idList) {
        return ResponseResult.ok(this.roadService.removeByIds(idList));
    }
}

